#include <iostream>
#include <stdlib.h>
#include "zsc.h"
#include "quest.h"
#include "game.h"
#include "changes.h"

//Kuldet inicializalasa
void Question::Set(int ssol, int sappr, int sxp, int sbolvl, std::string stask)
{
    solution = ssol;
    appr = sappr;
    xp = sxp;
    boredomLvl = sbolvl;
    task = stask;
}

void Question::putTask()
{
    std::cout << task << std::endl;
}

//TODO3 betoltes
//Megoldas bekerese, valtoztatasok letrehozasa
Changes Question::getSolution()
{
    int sol;
    Changes mod;
    mod.done=false;
    std::cout << std::endl << "A valasz?" << std::endl;
    std::cin >> sol;
    if (sol==solution)
    {
        mod.done=true;
        mod.boredom=boredomLvl;
        mod.xp=xp;
    }
    else if(solution-appr<=sol && solution+appr>=sol)
    {
        mod.done=true;
        mod.boredom=boredomLvl;
        mod.xp=xp/2;
    }
    return mod;
}

void Question::giveUp()
{ std::cout << "Feladtad!"; }


//Harc inicializalasa
void Fight::Set(std::string name, int bolvl, std::string sstory,int shp, int satk, int sxp)
{
    enemy = new Warrior(name);
    enemy->Set(satk,shp);
    boredomLvl = bolvl;
    story = sstory;
    xp = sxp;
}

//A harc jatekos altal vegzett fele
void Fight::autom(bool aut, Player* p)
{
        int c;
        if(!aut)
        {
            std::cout << "Tamad(1)/Kepesseget hasznal(2)" << std::endl;
            std::cin >> c;
        }
        else
        {
            c = rand() % 1 + 1;
        }
        if (c==1)
            enemy->Set(enemy->getAtk(),enemy->getDef()-p->getAtk());
        else if (c==2)
        {
            try {
            if (p->getSpellNum()>0)
            {
                int which;
                if(!aut)
                {
                        if (p->getSpellNum()>0)
                        {
                            for(int i=0;i<p->getSpellNum();++i)
                            {
                               // if (p->getSpecAbility(i).getAvailable())
                                    std::cout << p->getSpecAbility(i).getName() << "(" << i << ")" << " || ";
                            }
                            std::cin>>which;
                        }
                }
                else
                {
                    which = rand() % p->getAllSpell().size();
                }
                if (which>=0 && which<=p->getSpellNum())
                    enemy->Set(enemy->getAtk(),enemy->getDef()-p->getSpecAbility(which).getDmg());
            }
            else throw Game::NO_SPELL;
            } catch (Game::error e){

                    std::cout << "Meg nem jutott hozza egy kepesseghez sem!"<<std::endl;
            }
        }
}

void Fight::writeStory()
{
    std::cout << story << std::endl;
}

//A tenyleges harc
Changes Fight::fight(Player* p)
{
    std::cout << "Automatikus vagy manualis harc? (a/m)" << std::endl;
    char c;
    std::cin >> c;
    bool aut;
    int i=0;
    int fullHP=p->getDef();
    aut = (c=='a');
    Changes mod;
    if (!aut)
        {
            std::cout << "Jatekos HP:" << p->getDef() << " ||| " << "Ellenfel HP: " << enemy->getDef() << std::endl;
            std::cout << "Jatekos tamado ero:" << p->getAtk() << " ||| " << "Ellenfel tamado ero:" << enemy->getAtk() << std::endl;
        }

    while(enemy->getDef()>0 && p->getDef()>0)
    {
        // Jatekos kore
        autom(aut,p);
        //Ellenfel kore, random valaszt az akciok kozul
        int whatTD = rand() % 1;
        if (whatTD==0 && enemy->getDef()>0)
        {
            p->Set(p->getAtk(),p->getDef()-enemy->getAtk());
        }
        else if (enemy->getDef()>0)
        {
            int which = rand() % enemy->getAllSpell().size();
            p->Set(p->getAtk(),p->getDef()-enemy->getSpecAbility(which).getDmg());
        }
        std::cout << "Jatekos HP:" << p->getDef() << " ||| " << "Ellenfel HP: " << enemy->getDef() << std::endl;
        i++;
    }

    if (p->getDef()>0)
    {
        mod.done=true;
        mod.boredom=boredomLvl;
        std::cout << "Szerzett tapasztalati pont: " << xp+(i/2)<< std::endl;
        mod.xp=xp+(i/2);
    }
    else
    {
        std::cout << "Nem teljesitett kuldetes!" << std::endl;
        mod.done = false;
    }
        mod.def = fullHP;
    return mod;
}



void Quest::setP(Player* sp)
{
    p = sp;
}

void Quest::setQ(int ssol, int sappr, int sxp, int sbolvl, std::string stask) {
    q.Set(ssol, sappr, sxp, sbolvl, stask);
}

void Quest::setF(std::string name, int bolvl, std::string sstory,int shp, int satk, int sxp) {
    f.Set(name, bolvl, sstory, shp, satk, sxp);
}

void Quest::putText()
{
    std::cout << std::endl << text << std::endl;
}

//egy kuldetes vegigjatszasa
void Quest::askforType()
{
    char type;
    std::string str;
    do {
        std::cout << "Fejtoro vagy Harc? (f/h)" << std::endl;
        std::cin >> type;
        std::string str2;
        switch (type)
        {
            case 'f' :
                q.putTask();
                int v;
                do {
                    std::cout << "Valasz adasa(1) / Feladas(2)";
                    std::cin >> v;
                    if (v==1)
                    {
                        Changes temp = q.getSolution();
                        change.done = temp.done;
                        change.xp = temp.xp;
                        change.boredom = temp.boredom;
                        change.def = p->getDef();
                    }
                    else if (v==2)
                      {
                        q.giveUp();
                        change.def = p->getDef();
                        change.done = false;
                      }
                    std::cin.clear();
                    getline(std::cin,str2);
                } while(v!=1 && v!=2);
                break;

            case 'h' :
                f.writeStory();
                change = f.fight(p);
                break;
        }
    }while ( type!='f' && type!='h');
}

void Quest::putQuestion()        { q.putTask();    }
void Quest::Set(std::string str) { text = str;     }
void Quest::putFight()           { f.writeStory(); }
Changes Quest::getCh()           { return change;  }
void Quest::setjunc(bool j,int f,int s)
{
    junc = j;
    fjunc = f;
    sjunc = s;
}


